/****************************************************************************
 *
 *  ALi (Shanghai) Corporation, All Rights Reserved. 2002 Copyright (C)
 *
 *  File: lib_ash.c
 *
 *  Description: This file contains all functions definition ALi shell
 *		         operation functions.
 *  History:
 *      Date        Author      Version  Comment
 *      ====        ======      =======  =======
 *  1.  2003.7.17  Justin Wu    0.0.000  Initial
 *
 ****************************************************************************/

#include <types.h>
#include <retcode.h>
#include <sys_config.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <bus/sci/sci.h>
#include <hld/hld_dev.h>

#include "lib_ash.h"

#define LIB_ASH_COMMAND_LST			8
#define LIB_ASH_COMMAND_LEN			128

static unsigned char cmd_buffer[LIB_ASH_COMMAND_LEN];
static unsigned char *argv[LIB_ASH_COMMAND_LST];
static unsigned int argc;

#define ASH_TASK_STACKSIZE	0x8000//0x1000
#define ASH_TASK_QUANTUM		10
static ID ash_task_id = OSAL_INVALID_ID;
static BOOL m_exit_task = FALSE;

struct ash_cmd *ash_cm = NULL;

BOOL ash_task_init()
{
	//ID  			ash_task_id;
	OSAL_T_CTSK		t_ctsk;

	t_ctsk.stksz	= ASH_TASK_STACKSIZE;
	t_ctsk.quantum	= ASH_TASK_QUANTUM;
	t_ctsk.itskpri	= OSAL_PRI_NORMAL;
	t_ctsk.name[0]	= 'A';
	t_ctsk.name[1]	= 'S';
	t_ctsk.name[2]	= 'H';
	t_ctsk.task = (FP)lib_ash_shell_sfu_test;
	ash_task_id = osal_task_create(&t_ctsk);
	if(OSAL_INVALID_ID == ash_task_id)
	{
		PRINTF("cre_tsk ash_task_id failed\n");
		return FALSE;
	}

	return TRUE;
}

void ash_cm_register_sfu_test(struct ash_cmd *cm)
{
	ash_cm = cm;
};

static void lib_ash_get_command(void)
{
    unsigned char c;
    unsigned char *p, *q;

    p = cmd_buffer;
    q = cmd_buffer + LIB_ASH_COMMAND_LEN - 1;
    memset(cmd_buffer,0x0,sizeof(cmd_buffer));
    while (p < q)
    {
        /* Get a charactor and echo it */
        //LIB_ASH_OC(c = LIB_ASH_IC());
        c = LIB_ASH_IC();

        if (c == 0x0D || c == 0x0A)
        {
            q = p;
            break;
        }
        /* Backspace */
        else if (c == 0x08)
        {
            if (p != cmd_buffer)
                p--;
        }
        else
            *p++ = c;
    }

    argc = 0;
    p = cmd_buffer;
    while (p < q)
    {
        /* skip blank */
        while ((*p == ' ' || *p == '\t') && p < q)
            p++;
        /* new word */
        argv[argc++] = p;
        /* find end of word */
        while (*p != ' ' && p < q)
            p++;
        *p++ = 0;
    }
}

void sfu_ash_task_delete()
{
	if(OSAL_INVALID_ID != ash_task_id)
	{
        //osal_task_delete(ash_task_id);
        m_exit_task = TRUE;
        ash_task_id = OSAL_INVALID_ID;
	}
}
static void lib_ash_prompt(void)
{
    LIB_ASH_OC(0x0A);
    LIB_ASH_OC('>');
    LIB_ASH_OC(':');
}

void lib_ash_shell_sfu_test()
{
    unsigned int i;

    /* Command line process */
#ifdef   __MM_OSD_VSRC_MEM_ADDR  
    OSD_TaskBuffer_Init(osal_task_get_current_id(),__MM_OSD_VSRC_MEM_ADDR);
#endif
    m_exit_task = FALSE;
    while (1)
    {
        //lib_ash_prompt();
        lib_ash_get_command();

        if (argc == 0)
            continue;

	    i = 0;

	    if(NULL != ash_cm)
    	{
    	    do
    	    {  
    	        if(ash_cm[i].command)
    	        {
        		    if (STRCMP(ash_cm[i].command, argv[0]) == 0)
        		    {
        		        ash_cm[i].handler(argc, argv);
        		        break;
        		    }
        		    else
        			    i++;
    	        }
    		 else
    		    break;	        	
    	    }while(1);
    	}

        if(m_exit_task == TRUE)
            break;
    }
}

